package kr.ac.skhu.m.controller.neuti.pr;

import java.net.URL;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kr.ac.skhu.m.data.BoardData;
import kr.ac.skhu.m.data.SkhuNewsData;
import kr.ac.skhu.m.util.EncodingUtil;
import kr.ac.skhu.m.util.PaggingUtil;
import kr.ac.skhu.m.util.StringUtil;
import kr.ac.skhu.m.value.NoticeListValue;
import kr.ac.skhu.m.value.SkhuNewsValue;
import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.Source;

import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class SkhuNewsC extends AbstractController {
	
	private String mode;
	private String successView;
	private String bsid;
	
	public void setMode(String mode) {
		this.mode = mode;
	}

	public void setSuccessView(String successView) {
		this.successView = successView;
	}

	public void setBsid(String bsid) {
		this.bsid = bsid;
	}

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		ModelAndView mav = null;
		
		if(mode.equals("list")){
			mav = list(request, response);
		}else{
			mav = view(request, response);
		}
		
		return mav;
	}
	
	protected ModelAndView list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		ModelAndView mav = new ModelAndView(successView);
		
		//요청이 get방식인지 post방식인지 구분한다.
		boolean isGet = request.getMethod().equals("GET");
		
		/**
		 * 요청 파라미터
		 */
		//현재 페이지
		String currentPage = ServletRequestUtils.getStringParameter(request, "curPage", "1");
		//검색 타입
		String searchType = ServletRequestUtils.getStringParameter(request, "searchType", "1");
		//검색어
		String searchWord = isGet ? EncodingUtil.getParamterSafely(request.getQueryString(), "searchWord") : ServletRequestUtils.getStringParameter(request, "searchWord", "");
		//검색어를 ECU-KR로 인코딩
		String srchWord = URLEncoder.encode(searchWord, "EUC-KR");
		
		//URL 생성
		String url = "http://www.skhu.ac.kr/board/boardlist.aspx?bsid=" + bsid +"&curpage=" + currentPage + 
				"&searchType=" + searchType + "&searchWord=" + srchWord;
		
		/**
		 * 목록 생성
		 */
		SkhuNewsData boardData = new SkhuNewsData(url);
		List<SkhuNewsValue> valueList = boardData.getSkhuListValue();
		
		/**
		 * 페이징 정보
		 */
		int intTypeCurPage = boardData.getIntTypeCurPage();
		int intTypeTotalPage = boardData.getIntTypeTotalPage();
		PaggingUtil paggingUtil = new PaggingUtil(intTypeCurPage, intTypeTotalPage);
		
		mav.addObject("listSize", valueList.size());
		mav.addObject("valueList", valueList);
		mav.addObject("paggingUtil", paggingUtil);
		mav.addObject("searchType", searchType);
		mav.addObject("searchWord", searchWord);
		
		return mav;
	}
	
	protected ModelAndView view(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		ModelAndView mav = new ModelAndView(successView);
		
		boolean isGet = request.getMethod().equals("GET");
		
		String currentPage = ServletRequestUtils.getStringParameter(request, "curPage", "1");
		String searchType = ServletRequestUtils.getStringParameter(request, "searchType", "1");
		String searchWord = isGet ? EncodingUtil.getParamterSafely(request.getQueryString(), "searchWord") : ServletRequestUtils.getStringParameter(request, "searchWord", "");
		
		//송장 번호와 cj택배에 배송추적 관련 주소와 연결 시킨다.
		String url = "http://www.skhu.ac.kr/board/boardread.aspx?idx=" + request.getParameter("idx") + "&bsid=" + bsid + "&curpage=" + currentPage;

		//해당 URL 페이지를 가져온다.
		Source source = new Source(new URL(url));

		//메소드 찾기를 위해 시작부터 끝까지 태그들만 parse 한다 (?)
		source.fullSequentialParse();
		
		/**
		 * 내용
		 */
		Element element = source.getAllElementsByClass("board_view_con").get(0).getAllElements(HTMLElementName.SPAN).get(0);
		String content = element.toString();
		content = content.replace("href=\"", "href=\"http://www.skhu.ac.kr");
		content = content.replace("href='", "href='http://www.skhu.ac.kr");
		content = content.replace("src=\"", "src=\"http://www.skhu.ac.kr");
		content = content.replace("src='", "src='http://www.skhu.ac.kr");
		
		mav.addObject("content", content);
		mav.addObject("currentPage", currentPage);
		mav.addObject("searchType", searchType);
		mav.addObject("searchWord", searchWord);
		
		return mav;
	}
	
}
